package restless.core {
	
	import restless.core.gameObjs.RLEBlitMC;
	import restless.core.gameObjs.RLEManagedObj;
	import restless.core.interfaces.IAchieve;
	
	/**
	 * 
	 * @author Jordan Laine
	 * 
	 */
	
	public class RLEAchievement extends RLEManagedObj {
		
		private var _stat:RLEStat;
		private var _complete:Boolean;
		private var _goal:Number;
		private var _icon:RLEBlitMC;
		private var _iconBlitString:String;
		private var _trackHistory:Boolean;
		private var _trackSession:Boolean;
		private var _trackCurrent:Boolean;
		private var _negative:Boolean = false;
		private var _game:IAchieve;
		
		public function RLEAchievement(id:String) {
			setRE();
			sID = id;
			updating = true;
		}
		
		public function get stat():RLEStat {
			return _stat;
		}
		
		public function set stat(s:RLEStat):void {
			_stat = s;
			_negative = _stat.negative;
			update();
		}
		
		override public function set visible(bool:Boolean):void {
			_icon.visible = bool;
			_icon.x = x;
			_icon.y = y;
		}
		
		public function get negative():Boolean {
			return _negative;
		}
		
		public function set negative(bool:Boolean):void {
			_negative = bool;
		}
		
		public function get icon():RLEBlitMC {
			return _icon;
		}
		
		public function set icon(value:RLEBlitMC):void {
			_icon = value;
		}
		
		public function get iconBlitString():String {
			return _iconBlitString;
		}
		
		public function set iconBlitString(s:String):void {
			_iconBlitString = s;
			_icon = new RLEBlitMC();
			_icon.setRE();
			_icon.switchGFX(gfxM.getBDV(_iconBlitString), center);
			_icon.x = x;
			_icon.y = y;
			width = _icon.width;
			height = _icon.height;
			_icon.overlaying = true;
			_icon.rendering = true;	
		}
		
		public function get goal():Number {
			return _goal;
		}
		
		public function set goal(num:Number):void {
			_goal = num;
		}
		
		public function trackCurrent(bool:Boolean):void {
			_trackCurrent = true;
			_trackHistory = false;
			_trackSession = false;
		}
		
		public function trackHistory(bool:Boolean):void {
			_trackCurrent = false;
			_trackHistory = true;
			_trackSession = false;
		}
		
		public function trackSession(bool:Boolean):void {
			_trackCurrent = false;
			_trackHistory = false;
			_trackSession = true;
		}
		
		private function complete():void {
			trace("achievo cmoplete in the achievo, aka YO DAWG");
			updating = false;
			_complete = true;
			_stat.complete = true;
			_game.achievmentComplete(nID);
		}
		
		override public function update():void {
			if (visible) {
				_icon.x = x;
				_icon.y = y;
			}
			
			if (_trackCurrent) {
				if (!_negative) {
					if (_stat.currentCount >= _goal) { complete(); }
				} else {
					if (_stat.currentCount <= _goal) { complete(); }
				}
			} else if (_trackHistory) {
				if (!_negative) {
					if (_stat.historyCount >= _goal) { complete(); }
				} else {
					if (_stat.historyCount <= _goal) { complete(); }
				}
			} else if (_trackSession) {
				if (!_negative) {
					if (_stat.sessionCount >= _goal) { complete(); }
				} else {
					if (_stat.sessionCount <= _goal) { complete(); }
				}
			}
		}
		
		override public function dispose():void {
			_stat = null;
			_game = null;
			super.dispose();
		}
	}
}